package com.leetcode.partition4;

import java.util.*;

/**
 * @author `RKC`
 * @date 2021/9/9 12:21
 */
public class LC349两个数组的交集 {

    public static int[] intersection(int[] nums1, int[] nums2) {
        return hashtable(nums1, nums2);
    }

    public static void main(String[] args) {
        int[] nums1 = {4, 9, 5}, nums2 = {9, 4, 9, 8, 4};
        System.out.println(Arrays.toString(intersection(nums1, nums2)));
    }

    private static int[] hashtable(final int[] nums1, final int[] nums2) {
        Set<Integer> answerSet = new HashSet<>();
        Map<Integer, Integer> ht = new HashMap<>();
        for (int i = 0; i < nums1.length; i++) ht.put(nums1[i], 1);
        for (int i = 0; i < nums2.length; i++) {
            if (ht.containsKey(nums2[i])) {
                answerSet.add(nums2[i]);
            }
        }
        return answerSet.stream().mapToInt(val -> val).toArray();
    }
}
